package com.lf.api.exception;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.lf.api.controller.IndexController;
import com.lf.api.vo.MessageResult;
import com.lf.api.weixin.JacksonUtil;

@Component
public class DefaultExceptionHandler implements HandlerExceptionResolver {  
	private static Logger log = LoggerFactory.getLogger(IndexController.class);
    @Override  
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,  Exception ex) {    
        ModelAndView mv = new ModelAndView();  
        /*  使用response返回    */  
        response.setStatus(HttpStatus.OK.value()); //设置状态码  
        response.setContentType(MediaType.APPLICATION_JSON_VALUE); //设置ContentType  
        response.setCharacterEncoding("UTF-8"); //避免乱码  
        response.setHeader("Cache-Control", "no-cache, must-revalidate");  
        try {  
            response.getWriter().write(JacksonUtil.bean2JSon(MessageResult.SYSTEM_ERROR.setData(ex.getMessage())));  
        } catch (IOException e) {  
           log.error("Sytem error:{}", e.getMessage(), e);  
        }
        return mv;  
    }  
  
}  